package com.wechat.servlet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wechat.DAO.DaoImpl.FriendRequestDAOIpml;
import com.wechat.DAO.FriendRequestDAO;
import com.wechat.servlet.Utils.Base64Decoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ClearRqstServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        FriendRequestDAO friendRequestDAO = new FriendRequestDAOIpml();
        String data = request.getParameter("data");
        String RID = request.getParameter("RID");
        String json = Base64Decoder.decode(data);
        JSONArray jsonArray = JSONArray.parseArray(json);
        System.out.println(jsonArray);
        try {
            for(int i =0 ;i < jsonArray.size();i++){
                String SID = (String) jsonArray.get(i);
                friendRequestDAO.receiveOk(SID,RID);
                System.out.println("刚刚"+RID+"说他收到"+SID+"好友请求了");
            }
        }catch (Exception e){
            e.printStackTrace();
            response.getWriter().write("fail");
        }
        response.getWriter().write("success");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
